package permission

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"private-domain-overseas-service/admin/business/api/internal/repository/permission"
	"private-domain-overseas-service/admin/business/api/internal/svc"
	"private-domain-overseas-service/admin/business/api/internal/types"
	"private-domain-overseas-service/pkg/common"
	"private-domain-overseas-service/pkg/status/bizadmin"
	"private-domain-overseas-service/pkg/xerr"
	"private-domain-overseas-service/tools/ent/adminrole"
	"private-domain-overseas-service/tools/ent/adminrouter"
	"private-domain-overseas-service/tools/ent/predicate"
	"strconv"
)

type AdminRouterDeleteLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// NewAdminRouterDeleteLogic - 后台菜单删除接口
func NewAdminRouterDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminRouterDeleteLogic {
	return &AdminRouterDeleteLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// AdminRouterDelete - 后台菜单删除接口
func (l *AdminRouterDeleteLogic) AdminRouterDelete(req *types.AdminRouterDeleteRequest) error {
	adminRouterDao := permission.NewAdminRouterDao(l.ctx, l.svcCtx)
	adminRoleDao := permission.NewAdminRoleDao(l.ctx, l.svcCtx)

	// 查询路由
	conditions := make([]predicate.AdminRouter, 0, 1)
	conditions = append(conditions, adminrouter.IDEQ(int(req.Id)))
	originRouter, err := adminRouterDao.AdminRouterFirst(conditions)
	if err != nil {
		return xerr.ErrCodeMsg(xerr.ServerOperationExceptionError, fmt.Sprintf("查询路由失败: %v", err.Error()))
	}

	// 检查路由是否有孩子
	conditions = make([]predicate.AdminRouter, 0, 1)
	conditions = append(conditions, adminrouter.AdminRouterPidEQ(req.Id))
	adminRouters, _, err := adminRouterDao.AdminRouterConditionsAll(conditions)
	if err != nil {
		return xerr.ErrCodeMsg(xerr.ServerOperationExceptionError, fmt.Sprintf("查询路由失败: %v", err.Error()))
	}

	// 存在子菜单，不允许删除
	if len(adminRouters) > 0 {
		return xerr.ErrCodeMsg(xerr.ServerAdminRouterHasChild, "存在子菜单，不允许删除")
	}

	// 删除路由
	conditions = make([]predicate.AdminRouter, 0, 1)
	conditions = append(conditions, adminrouter.IDEQ(int(req.Id)))
	err = adminRouterDao.AdminRouterDelete(conditions)
	if err != nil {
		return xerr.ErrCodeMsg(xerr.ServerOperationExceptionError, fmt.Sprintf("删除路由失败: %v", err.Error()))
	}

	// 更新普通用户 和 超级管理员权限
	if originRouter.AdminRouterSystemType == bizadmin.TypeBase {
		// 查询普通用户角色
		adminRoleConditions := make([]predicate.AdminRole, 0, 2)
		adminRoleConditions = append(adminRoleConditions, adminrole.AdminRoleTypeEQ(bizadmin.TypeNormal))
		adminRoleConditions = append(adminRoleConditions, adminrole.IsDeleteEQ(bizadmin.NotDelete))
		adminRole, err := adminRoleDao.AdminRoleFirst(adminRoleConditions)
		if err != nil {
			return xerr.ErrCodeMsg(xerr.ServerOperationExceptionError, "查询普通用户角色失败")
		}

		// 更新
		adminRole.AdminRoleRouterIds = common.DeleteStr(adminRole.AdminRoleRouterIds, strconv.Itoa(int(req.Id)))
		_ = adminRoleDao.AdminRoleUpdate(adminRole, nil)
	} else {
		// 查询超级管理员角色
		adminRoleConditions := make([]predicate.AdminRole, 0, 2)
		adminRoleConditions = append(adminRoleConditions, adminrole.AdminRoleTypeEQ(bizadmin.TypeSuper))
		adminRoleConditions = append(adminRoleConditions, adminrole.IsDeleteEQ(bizadmin.NotDelete))
		adminRole, err := adminRoleDao.AdminRoleFirst(adminRoleConditions)
		if err != nil {
			return xerr.ErrCodeMsg(xerr.ServerOperationExceptionError, "查询超级管理员角色失败")
		}

		// 更新
		adminRole.AdminRoleRouterIds = common.DeleteStr(adminRole.AdminRoleRouterIds, strconv.Itoa(int(req.Id)))
		_ = adminRoleDao.AdminRoleUpdate(adminRole, nil)
	}

	return nil
}
